package com.farsunset.cim.sdk.server.slc.group;

import com.farsunset.cim.constant.ChannelAttr;
import com.farsunset.cim.group.SessionGroup;
import com.farsunset.cim.model.Message;
import com.farsunset.cim.sdk.server.slc.utils.ChannelWriteTransform;
import io.netty.channel.Channel;

import java.util.Collection;
import java.util.function.Predicate;

public class SlcSessionGroup extends SessionGroup {

    @Override
    public void write(String key, Message message) {
        for (Channel channel :
                find(key)) {
            channel.writeAndFlush(ChannelWriteTransform.transformWriteObj(channel, message));
        }
    }

    @Override
    public void write(String key, Message message, Predicate<Channel> matcher) {
        find(key).stream().filter(matcher).forEach(channel -> channel.writeAndFlush(ChannelWriteTransform.transformWriteObj(channel, message)));
    }

    @Override
    public void write(String key, Message message, Collection<String> excludedSet) {
        find(key).stream().filter(channel -> excludedSet == null || !excludedSet.contains(channel.attr(ChannelAttr.UID).get())).forEach(channel -> channel.writeAndFlush(ChannelWriteTransform.transformWriteObj(channel, message)));
    }

}